<template>
  <div>
    <!-- 面包屑导航区域 -->
    <breadcrumb :breadcrumbs="breadcrumbs" />
    <el-card>
      <!-- 添加按钮 -->
      <el-row>
        <el-col>
          <el-button type="primary">添加</el-button>
        </el-col>
      </el-row>
      <!-- 表格数据 -->
      <el-table :data="rolesList" border stripe>
        <el-table-column type="expand">
          <template v-slot="scope">
            <el-row
              v-for="(item1, i1) in scope.row.children"
              :key="i1"
              :class="['bdbottom', 'vcenter', i1 == 0 ? 'bdtop' : '']"
            >
              <!-- 渲染一级权限 -->
              <el-col :span="5">
                <el-tag closable @close="removeRightById(scope.row, item1.id)">
                  {{ item1.authName }}
                </el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>

              <!-- 渲染二，三级权限 -->
              <el-col :span="19">
                <!-- 通过for循环嵌套渲染二级权限 -->
                <el-row
                  v-for="(item2, i2) in item1.children"
                  :key="i2"
                  :class="[i2 == 0 ? '' : 'bdtop', 'vcenter']"
                >
                  <el-col :span="6">
                    <el-tag
                      type="success"
                      closable
                      @close="removeRightById(scope.row, item2.id)"
                    >
                      {{ item2.authName }}
                    </el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <!-- 通过for循环嵌套渲染三级权限 -->
                    <el-tag
                      type="warning"
                      v-for="(item3, i3) in item2.children"
                      :key="i3"
                      closable
                      @close="removeRightById(scope.row, item3.id)"
                    >
                      {{ item3.authName }}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="editRoleClick(scope.row.id)"
            >
              编辑
            </el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete">
              删除
            </el-button>
            <el-button
              size="mini"
              type="warning"
              icon="el-icon-setting"
              @click="showSetRightDialogClick(scope.row)"
            >
              分配权限
            </el-button>
          </template>
        </el-table-column>
      </el-table>

     
    </el-card>

    <!-- 分配权限弹框 -->
    <el-dialog title="分配权限" :visible="setRightDialogVisible" width="50%">
      <!-- 树形控件 -->
      <el-tree
        :data="rightsList"
        :props="treeProps"
        show-checkbox
        node-key="id"
        default-expand-all
        :default-checked-keys="defKeys"
        ref="treeRef"
      ></el-tree>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="setRightDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="allotRights"> 确 定 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import Breadcrumb from "@/components/Breadcrumb";

import { getRoles, deleteRightById, addRights } from "@/network/power/roles";
import { getRightsList } from "@/network/power/rights";

export default {
  name: "Roles",
  data() {
    return {
      breadcrumbs: ["权限管理", "角色列表"],
      rolesList: [], //角色列表数据
      setRightDialogVisible: false,
      rightsList: [], //所有权限的数据
      //树形对象的绑定数据
      treeProps: {
        label: "authName",
        children: "children",
      },
      //默认选中的节点id
      defKeys: [105],
      roleId: "", //选中的角色id
    };
  },
  components: {
    Breadcrumb,
  },
  methods: {
    getRolesList() {
      getRoles().then((res) => {
        if (res.meta.status != 200)
          return this.$message.error("获取角色列表信息失败！" + res.meta.msg);
        this.rolesList = res.data;
        this.$message.success("获取角色列表信息成功！");
      });
    },
    //根据id删除对应的权限
    removeRightById(rale, rightId) {
      //  弹框提示
      this.$confirm("此操作将永久删除该角色权限, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteRightById(rale.id, rightId).then((res) => {
            if (res.meta.status != 200)
              return this.$message.error("删除失败！" + res.meta.msg);
            this.$message.success("删除权限成功");
            // this.getRolesList();
            rale.children = res.data;
          });
        })
        .catch(() => {
          this.$message.info("已取消删除！");
        });
    },
    //设置权限弹框
    showSetRightDialogClick(role) {
      getRightsList("tree")
        .then((res) => {
          if (res.meta.status != 200)
            return this.$message.error("获取权限数据失败！" + res.meta.msg);
          this.rightsList = res.data;
          this.$message.success("获取权限数据成功！");
          console.log(res.data);
        })
        .catch((error) => {
          this.$message.error("获取权限数据失败！" + error);
        });
      this.roleId = role.id; //保存id
      this.defKeys = []; //保存之前先清空
      this.getLeadKeys(role, this.defKeys);
      this.setRightDialogVisible = true;
    },

    //递归获取三级权限id
    getLeadKeys(node, arr) {
      if (!node.children) {
        return arr.push(node.id);
      }
      node.children.forEach((item) => {
        this.getLeadKeys(item, arr);
      });
    },
    //分配权限
    allotRights() {
      const keys = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys(),
      ];
      const idStr = keys.join(",");
      //发起请求
      addRights(this.roleId, idStr).then((res) => {
        if (res.meta.status != 200)
          return this.$message.error("分配权限失败！" + res.meta.msg);
        this.$message.success("分配权限成功！");
      });
      setTimeout(() => {
        this.getRolesList();
      }, 1000);
      this.setRightDialogVisible = false;
    },
  },
  created() {
    this.getRolesList();
  },
};
</script>

<style lang="less" scoped>
.el-tag {
  margin: 7px;
}
.bdtop {
  border-top: 1px solid #eee;
}
.bdbottom {
  border-bottom: 1px solid #eee;
}
.vcenter {
  display: flex;
  align-items: center;
}
</style>>
